package com.demo.datastructure.stack;

class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.isValid("()");
    }

    public boolean isValid(String s) {
        String one = "(";
        String one2 = ")";  
        String two = "{";
        String two2 = "}";  
        String three = "[";
        String three2 = "]";  
        Stack stack = new Stack();
        for(int i = 0; i < s.length(); i++){
            String news = s.substring(i,i+1); 
            if(news.equals(one) || news.equals(two) || news.equals(three)){
                   stack.pop(news);
            }

            if(news.equals(one2)){
                   stack.put(one);
            }

            if(news.equals(two2)){
                stack.put(two);
            }

            if(news.equals(three2)){
                stack.put(three);
            }
        }      

        if(stack.getCount() == 0){
            System.out.println("1");
            return true;
        }
        System.out.println("0");
        return false;
    }

    private class Stack{
        private String[] args = new String[10];
        private int count;
        private int length = 10;

        public Integer getCount(){
            return count;
        }    


        public void pop(String symbol){
            if(++count == length){
                grow();
            }  
            args[length-count] = symbol;   
        }

        public void put(String symbol){
            if(!args[length-count].equals(symbol)){
                return;
            } 
            args[length-count] = null;   
            count--; 
        }


        private void grow(){
            int oldLength = length;
            String[] newArgs = new String[length = length*2];
            for(int i = 0; i < oldLength; i++){
                  newArgs[oldLength+i] = args[i]; 
            }
            args = newArgs;
        }



    }
}